package com.java.module.user.controller.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.java.common.lang.annotation.Log;
import com.java.common.lang.domain.AjaxResult;
import com.java.common.lang.domain.entity.User;
import com.java.common.lang.domain.page.TableDataInfo;
import com.java.common.lang.enums.BusinessTypeEnum;
import com.java.common.utils.WebKit;
import com.java.common.utils.poi.ExcelUtil;
import com.java.module.user.service.IUserService;

/**
 * 用户模块：用户信息Controller
 * 
 * @author Administrator
 * @date 2021-02-23
 */
@RestController("AdminUserController")
@RequestMapping("/user/user")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 查询用户模块：用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('user:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(User user) {
        WebKit.startPage();
        List<User> list = userService.selectUserList(user);
        return WebKit.getDataTable(list);
    }

    /**
     * 导出用户模块：用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('user:user:export')")
    @Log(title = "用户模块：用户信息", businessType = BusinessTypeEnum.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(User user) {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        return util.exportExcel(list, "user");
    }

    /**
     * 获取用户模块：用户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('user:user:query')")
    @GetMapping(value = "/{uid}")
    public AjaxResult getInfo(@PathVariable("uid") Long uid) {
        return AjaxResult.success(userService.selectUserById(uid));
    }

    /**
     * 新增用户模块：用户信息
     */
    @PreAuthorize("@ss.hasPermi('user:user:add')")
    @Log(title = "用户模块：用户信息", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody User user) {
        return WebKit.toAjax(userService.insertUser(user));
    }

    /**
     * 修改用户模块：用户信息
     */
    @PreAuthorize("@ss.hasPermi('user:user:edit')")
    @Log(title = "用户模块：用户信息", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user) {
        return WebKit.toAjax(userService.updateUser(user));
    }

    /**
     * 删除用户模块：用户信息
     */
    @PreAuthorize("@ss.hasPermi('user:user:remove')")
    @Log(title = "用户模块：用户信息", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/{uids}")
    public AjaxResult remove(@PathVariable Long[] uids) {
        return WebKit.toAjax(userService.deleteUserByIds(uids));
    }

}